package com.groupnections.client.Entities;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.googlecode.objectify.Key;
import com.groupnections.client.GlobalVariables;
import com.groupnections.shared.Entity;

public class FriendsPanel extends VerticalPanel {

	public FriendsPanel(Entity entity) {

		this.setSize("100%", "100%");

		for (Key<Entity> entityKey : entity.getEntitiesRelated()) {

			Entity entityConnected = GlobalVariables.entitiesMap.get(entityKey
					.getName());

			if (entityConnected != null
					&& Entity.USER_TYPE.equals(entityConnected.getEntityType())) {

				// HorizontalPanel hp = new HorizontalPanel();

				// hp.setSpacing(7);

				this.add(getFriend(entityConnected));
			}

		}
	}

	private VerticalPanel getFriend(Entity entity) {

		VerticalPanel vp = new VerticalPanel();

		vp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		vp.add(getImage(entity));
		// vp.add(new Label(entity.getEntityName()));

		return vp;
	}

	private Anchor getImage(final Entity entity) {

		Anchor image = new Anchor("<img src='" + GlobalVariables.IMAGE_FOLDER
				+ entity.getEntityID()
				+ ".jpg' width='44px' heigth='37px' ><br>"
				+ entity.getEntityName(), true);

		image.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				new InitializeEntity(entity);
			}
		});

		return image;
	}

}
